//
//  NationRepairTableViewCell.m
//  ICrEAM
//
//  Created by Cty on 2016/11/03.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "NationRepairTableViewCell.h"
#import "NationRepairHistoryDetailView.h"

#define CellHeight ActualHeight(88)

@interface NationRepairTableViewCell ()

@property (nonatomic, strong) UIView *cellView;

@property (nonatomic, strong) UILabel *createDateLabel;

@property (nonatomic, strong) UILabel *mileageLabel;

@property (nonatomic, strong) UILabel *repairTypeLabel;

@property (nonatomic, strong) ICMRepairOrderModel *repairModel;

@property (nonatomic, strong) NationRepairHistoryDetailView *detailView;
@property(nonatomic,weak)UIButton *showdetailButton;
@end

@implementation NationRepairTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        UIView *cellView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, CellHeight)];
        cellView.backgroundColor = BackgroundColor_White;
        [self.contentView addSubview:cellView];
        
        UILabel *createDateLabel = [self createLabelWithText:@"开单日期" frame:CGRectMake(0, 0, ActualWidth(260), CellHeight)];
        [cellView addSubview:createDateLabel];
        
        UIView *firstSep = [[UIView alloc] initWithFrame:CGRectMake(createDateLabel.rightEdge, 0, 1, CellHeight)];
        firstSep.backgroundColor = [UIColor whiteColor];
        [cellView addSubview:firstSep];
        
        UILabel *mileageLabel = [self createLabelWithText:@"进厂行驶里程" frame:CGRectMake(firstSep.rightEdge, 0, ActualWidth(262), CellHeight)];
        [cellView addSubview:mileageLabel];
        
        UIView *secondSep = [[UIView alloc] initWithFrame:CGRectMake(mileageLabel.rightEdge, 0, 1, CellHeight)];
        secondSep.backgroundColor = [UIColor whiteColor];
        [cellView addSubview:secondSep];
        
        UILabel *repairTypeLabel = [self createLabelWithText:@"维修类型" frame:CGRectMake(secondSep.rightEdge, 0, ActualWidth(300), CellHeight)];
        [cellView addSubview:repairTypeLabel];
        
        
        UIButton *detailButton = [[UIButton alloc] initWithFrame:CGRectMake(repairTypeLabel.rightEdge+ActualWidth(85), 0, ActualWidth(115), CellHeight)];
        [detailButton setImage:[UIImage imageNamed:GetCar_Expand] forState:UIControlStateNormal];
//        [detailButton setImage:[UIImage imageNamed:@"收起"] forState:UIControlStateSelected];
        [detailButton setImageEdgeInsets:UIEdgeInsetsMake((CellHeight-50)/2, (ActualWidth(115)-50)/2, (CellHeight-50)/2, (ActualWidth(115)-50)/2)];
        [detailButton addTarget:self action:@selector(clickDetailButton:) forControlEvents:UIControlEventTouchUpInside];
        [cellView addSubview:detailButton];
        self.showdetailButton=detailButton;
        UIView *horizontalSep = [[UIView alloc] initWithFrame:CGRectMake(0 , CellHeight-1, ScreenWidth, 1)];
        horizontalSep.backgroundColor = [UIColor whiteColor];
        [cellView addSubview:horizontalSep];
        
        self.cellView = cellView;
        self.createDateLabel = createDateLabel;
        self.mileageLabel = mileageLabel;
        self.repairTypeLabel = repairTypeLabel;
    }
    
    return self;
}

#pragma mark 点击展开按钮
- (void)clickDetailButton:(UIButton *)sender {
//    sender.selected = !sender.selected;
//    self.repairModel.isShow = sender.selected;
    if ([self.delegate respondsToSelector:@selector(showRepairDetail:)]) {
        [self.delegate showRepairDetail:self];
    }
}

#pragma mark 根据model设值
- (void)setupLabelsWithModel:(ICMRepairOrderModel *)model{
    self.repairModel = model;
//    self.createDateLabel.text = [ICrEAMTool getDateWithTimeInterval: model.RO_CREATE_DATE];
    self.createDateLabel.text = model.RO_CREATE_DATE;
    self.mileageLabel.text = model.IN_MILEAGE;
    self.repairTypeLabel.text = model.RO_REPAIR_TYPE_NAME;
    
    CGFloat detailViewHeight = ActualHeight(46)+ActualHeight(108)*ceilf(model.TT_RO_LABOUR.count/4.0)+(model.TT_RO_REPAIR_PART.count>0? ActualHeight(46)+ActualHeight(108)*ceilf(model.TT_RO_REPAIR_PART.count/4.0):0);
    if (model.isShow) {
        self.cellView.backgroundColor = TitleColor;
        self.createDateLabel.textColor = BackgroundColor_White;
        self.mileageLabel.textColor = BackgroundColor_White;
        self.repairTypeLabel.textColor = BackgroundColor_White;
        if (self.detailView == nil) {
            self.detailView = [NationRepairHistoryDetailView createNationRepairHistoryDetailViewWithModel:model andFrame:CGRectMake(0, self.cellView.bottomEdge, ScreenWidth, detailViewHeight)];
        }
    
        [self.showdetailButton setImage:[UIImage imageNamed:@"收起"] forState:UIControlStateNormal];
        [self.contentView addSubview:self.detailView];
    } else {
        [self.showdetailButton setImage:[UIImage imageNamed:GetCar_Expand] forState:UIControlStateNormal];
        self.cellView.backgroundColor = BackgroundColor_White;
        self.createDateLabel.textColor = ContentTextColor_Black;
        self.mileageLabel.textColor = ContentTextColor_Black;
        self.repairTypeLabel.textColor = ContentTextColor_Black;
        [self.detailView removeFromSuperview];
        self.detailView = nil;
    }
}

- (UILabel *)createLabelWithText:(NSString *)text frame:(CGRect)frame {
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    label.text = text;
    label.font = NaviFont;
    label.textColor = ContentTextColor_Black;
    label.textAlignment = NSTextAlignmentCenter;
    return label;
}

@end
